const db = wx.cloud.database()
let _id = '',
    wordName = {},
    inputText = '',
    audio = wx.createInnerAudioContext({
        useWebAudioImplement: true
    });
Page({

    /**
     * 页面的初始数据
     */
    data: {
        DataList: [],
        inputText: '',
        backShow: false,
        cardShow: false,
        is_play: false
    },

    onReady: function () {
        // 绑定组件id，用于后面调用指定id组件内的方法
        this.component = this.selectComponent("#popup")
    },
    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        this.getList()
    },

    //播放
    playmusic(address) {
        audio.src = address
        audio.play();
        console.log(address)
        // audio.onStop()
        console.log("继续播放")
    },

    async music(e) {
        // 执行组件内方法，让组件内的元素显示
        this.component.show()
        _id = e.currentTarget.dataset.val;
        let {
            result
        } = await wx.cloud.callFunction({
            name: 'getDictWord',
            data: {
                type: 'audioStatus',
                id: _id
            }
        })
        wordName = result[0]
        console.log(wordName)
        // console.log(result[0].address)
        if (wordName.address) { // TODO 悬浮页
            this.playmusic(wordName.address)
            this.setData({
                cardShow: true,
                word: wordName
            })
        }

    },

    /**
     * 停止音频文件
     */
    stopAudio() {
        audio.stop()
    },

    stopvdo() { // TODO  
        this.stopAudio()
        // this.getList()
    },

    // 获取数据
    async getList() {
        let {
            result
        } = await wx.cloud.callFunction({
            name: "getDictWord",
            data: {
                type: "amnCheck"
            }
        })
        this.setData({
            DataList: result
        })
        console.log(result)
        let checkAdo = result.find(item => {
            return item.status == 'checking'})
        if (!checkAdo) {
            wx.showToast({
                title: '暂时没有需要审核的音频哦',
                icon: 'none'
            })
        }
    },

    wordDetail(e) {
        console.log(e)
        let itm = e.currentTarget.dataset.val
        wx.navigateTo({ // 暂定为跳转提示页面
            url: '/pages/dictionary/wordDetail/wordDetail?id=' + itm._id + '&content=' + itm.content,
        })
    },

    // 上传
    async upWord() {
        let that = this
        wx.showModal({
            title: '是否上传此音频',
            success(res) {
                if (res.confirm) { // 确认
                    wx.showLoading({
                        title: '正在上传',
                    })
                    wx.cloud.callFunction({
                        name: "adminUpFiles",
                        data: {
                            type: 'success',
                            currword: wordName.content,
                            path: wordName.address,
                            lyric: wordName.lyric,
                            user: wordName.user,
                        }
                    }).then(() => {
                        wx.showToast({
                            title: '上传成功',
                            icon: "none"
                        })
                        wx.hideLoading({})
                        that.getList() // 刷新列表
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },

    back() {
        this.setData({
            backShow: true
        })
    },

    inputVal(e) {
        inputText = e.detail.value
    },
    async subReason() {
        let that = this
        console.log(inputText, wordName.content)
        if (inputText.length > 0) {
            await wx.cloud.callFunction({
                name: "adminUpFiles",
                data: {
                    type: 'fail',
                    currword: wordName.content,
                    reason: inputText
                }
            }).then(() => {
                wx.showToast({
                    title: '提交成功',
                    icon: "none"
                })
                that.getList() // 刷新列表
            })
        } else {
            wx.showToast({
                title: '请输入退回原因',
                icon: 'none'
            })
        }

    },
})